/**   
 * @Title: Main.java 
 * @Package org.playgod1984.du.ddesign.model 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author playgod playgod1984@163.com  
 * @date 2014年10月27日 下午4:00:43 
 * @version V1.0   
 */
package org.playgod1984.du.ddesign.model;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

/** 
 * @ClassName: Main 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author playgod playgod1984@163.com 
 * @date 2014年10月27日 下午4:00:43 
 *  
 */
@SuppressWarnings("all")
public class Main extends Model<Main> {
	public static final Main dao = new Main();
	/**
	 * @Title: getProject
	 * @author:playgod playgod1984@163.com 
	 * @Description: 获取当前用户的项目
	 * @param @return    设定文件 
	 * @return List<Record>    返回类型 
	 * @throws
	 */
	public List<Map> getProject(String loginaccout){
		List<Record> project =  Db.find("SELECT pro.projectcode ,pro.projectname  FROM dud_ddesign_pro pro"+
						" INNER JOIN dud_ddesign_pu pu"+
						" ON pro.projectcode = pu.projectcode"
						+" where pu.loginaccount=?",loginaccout);
		List<Map> projectList = new ArrayList<Map>();
		String sqldaig="SELECT diagramid,diagramname FROM dud_design_diagram"+
				" WHERE projectid=?";
		String sqltable="SELECT tablelname,tablepname,projectid FROM dud_design_table"+
				" WHERE diagramid=?";
		if(project.size()>0){// 之所以不判断null，因为Db.find返回永远不为null			
			for(Record proj:project){
				Map pro = new LinkedHashMap();
				pro.put("id", proj.getStr("projectcode"));
				pro.put("text", proj.getStr("projectname"));
				pro.put("type", "P");// 项目
				List<Record> diagram = Db.find(sqldaig, proj.getStr("projectcode"));
				if(diagram.size()>0){
					List<Map> diagramList = new ArrayList<Map>();
					for(Record diag:diagram){
						Map dia = new LinkedHashMap();
						dia.put("id", diag.getStr("diagramid"));
						dia.put("text", diag.getStr("diagramname"));
						dia.put("type", "D");// 图表
						List<Record> table = Db.find(sqltable, diag.getStr("diagramid"));						
						if(table.size()>0){
							List<Map> tableList = new ArrayList<Map>();
							for(Record tab : table){
								Map tbl = new LinkedHashMap();
								tbl.put("id", tab.getStr("tableid"));
								tbl.put("text", tab.getStr("tablelname"));
								tbl.put("proid", tab.getStr("projectid"));
								tbl.put("type", tab.getStr("T"));
								tableList.add(tbl);
							}
							dia.put("children", tableList);
						}
						diagramList.add(dia);
					}
					pro.put("children", diagramList);
				}
				projectList.add(pro);
			}
		}		
		return projectList;
	}
}
